﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using System.Web.Security;
using System.Web.Services;
using System.IO;

namespace FilStartUps.Account
{
    public partial class Login : System.Web.UI.Page
    {
        public string RootUrl
        {
            get
            {
                Uri requestUri = Context.Request.Url;
                HttpRequest request = Context.Request;
                string rootUrl = string.Format("{0}{1}{2}{3}{4}",
                    requestUri.Scheme,
                    Uri.SchemeDelimiter,
                    requestUri.Host,
                    requestUri.IsDefaultPort ? string.Empty : string.Format(":{0}", requestUri.Port),
                    request.ApplicationPath);
                return rootUrl.EndsWith("/") ? rootUrl : string.Format("{0}/", rootUrl);
            }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!Page.IsPostBack)
            {

            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {

            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {

                var check_user = (from m in db_connect.Members
                                  join r in db_connect.Roles
                                  on m.roleidFK equals r.roleidPK
                                  where m.emailaddress.Equals(txtEmail.Text.Trim())
                                  && m.password.Equals(txtPassword.Text.Trim())
                                  && m.member_flag.Equals(true)
                                  select m).SingleOrDefault();

                if (check_user != null)
                {
                    //Redirect Home with Cookies
                    string role = string.Empty;

                    if (check_user.roleidFK == 1)
                    {
                        role = "Moderator";
                    }
                    else
                    {
                        role = "Member";
                    }
                    //Create FormsAuthenticationTicket
                    FormsAuthenticationTicket tik = new FormsAuthenticationTicket(
                        1,
                        check_user.emailaddress,
                        DateTime.Now,
                        DateTime.Now.AddHours(24),
                        false,
                        role,
                        FormsAuthentication.FormsCookiePath);

                    string hash = FormsAuthentication.Encrypt(tik);

                    HttpCookie cookie = new HttpCookie(
                        FormsAuthentication.FormsCookieName,
                        hash);

                    if (tik.IsPersistent) cookie.Expires = tik.Expiration;

                    Response.Cookies.Add(cookie);

                    string returnUrl = this.Request.QueryString.Get("ReturnUrl");
                    if (!string.IsNullOrEmpty(returnUrl))
                    {
                        if (!returnUrl.ToLower().Contains("htpp://"))
                        {
                            returnUrl = Path.Combine(this.RootUrl, returnUrl);
                            string url = string.Format("{0}?ReturnUrl={1}", this.Request.Path, Server.UrlEncode(returnUrl));
                            this.Response.Redirect(url);
                        }
                    }
                    else
                    {
                        Response.Redirect("~/Home.aspx");
                    }
                }
              
            }
        }

     
    }
}